package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.model.dto.SaveTeachPlanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Date:2023/7/10
 * Author:丐版小杨哥
 * Description:
 */
@Slf4j
@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    private TeachplanMapper teachplanMapper;
    @Override
    public List<TeachplanDto> getTreeNodes(Long id) {
        return teachplanMapper.selectTreeNodes(id);
    }

    @Override
    public void saveTeachPlan(SaveTeachPlanDto planDto) {
        if (planDto.getId()==null){
            //添加章节
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(planDto,teachplan);
            Long courseId = planDto.getCourseId();
            Long parentid = planDto.getParentid();
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Teachplan::getCourseId,courseId).eq(Teachplan::getParentid,parentid);
            Integer integer = teachplanMapper.selectCount(wrapper);
            teachplan.setOrderby(integer+1);
            teachplanMapper.insert(teachplan);
        }else {
            //修改
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(planDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }
}